package com.xysoft.admin.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xysoft.admin.service.IHomeService;
import com.xysoft.model.User;
import com.xysoft.util.RequestUtil;

@Controller
@RequestMapping(value = "/admin/index.jspx")
public class HomeController extends BaseController{
	private IHomeService homeService;
	
	@RequestMapping(params = "login")
	public String login(HttpServletResponse response, HttpServletRequest request, 
			HttpSession session, User user) throws IOException {
		String res = this.homeService.login(user, request, session);
		if (res == null) {
			RequestUtil.go("login.html", "用户名或者密码错误！", request, response);
		} else RequestUtil.go("index.jsp", "登录成功！", request, response);
		return null;
	}
	
	@RequestMapping(params = "menu")
	public String menu(HttpServletResponse response, HttpServletRequest request, 
			HttpSession session) throws IOException {
		String res = this.homeService.menu(request, session);
		response.getWriter().print(res);
		return null;
	}
	
	@RequestMapping(params = "userinfo")
	@ResponseBody
	public Map<String,? extends Object> remove(HttpServletResponse response, HttpServletRequest request, 
			HttpSession session) throws IOException {
		return this.homeService.userinfo(request, session);
	}
	
	@RequestMapping(params = "logout")
	@ResponseBody
	public Map<String,? extends Object> logout(HttpServletResponse response, HttpServletRequest request, 
			HttpSession session) throws IOException {
		return this.homeService.logout(session);
	}

	@Autowired
	public void setHomeService(IHomeService homeService) {
		this.homeService = homeService;
	}
}
